import Im from 'immutable'
import {combineReducers} from 'redux'
import cr from 'utils/createReducer'
import {
  RECEIVE_FILM_DETAIL,
  INPUT_SHORT_COMMENT,
  ESCAPE_FILM_DETAIL,
  INSERT_SHORT_COMMENT,
  COMMENT_MAXIMUM
} from 'actions/consts'


const writingComment = combineReducers({
  text: cr('', {
    [INPUT_SHORT_COMMENT](state, {text}){return text},
    [INSERT_SHORT_COMMENT](){return ''},
    [ESCAPE_FILM_DETAIL](){return ''}
  }),
  exceed: cr(false, {
    [INPUT_SHORT_COMMENT](state, {text}){return text.length > COMMENT_MAXIMUM},
    [INSERT_SHORT_COMMENT](){return false},
    [ESCAPE_FILM_DETAIL](){return false}
  })
})

const info = cr({}, {
  [RECEIVE_FILM_DETAIL](state, {data}){ return data },
  [ESCAPE_FILM_DETAIL](){ return {} }
})

const commentList = cr(Im.List(), {
  [INSERT_SHORT_COMMENT](state, {id, time, content}){
    return state.push(Im.fromJS({id, time, content}))
  },
  [ESCAPE_FILM_DETAIL](){return Im.List()}
})

export default combineReducers({
  info,
  commentList,
  writingComment
})
